<?php
Class ReturndiscussAction extends Action{
	public function index(){
		$condition['cashtype']=4;
		import('ORG.Util.EditPage');
		$count = M('MedicalRecordsDiscuss')->where($condition)->count();
		$Page = new Page($count,10);
		$Page->setConfig('theme',"<ul class='pagination'><li><a>%totalRow% %header% %nowPage%/%totalPage% 页</a></li><li>%upPage%</li><li>%first%</li><li>%prePage%</li><li>%linkPage%</li><li>%nextPage%</li><li>%end%</li><li>%downPage%</li>".$gopagenu."</ul>");
		$this->page = $Page->show();
		$this->list = M()->Table('box_medical_records_discuss a')->join('box_user_order_detail b on a.order_num = b.order_num')->field('a.id,a.rec_id,a.addtime,b.order_num,b.points,b.fee,b.refund_id,c.name,d.name ename')->join('box_user c on a.openid=c.openid')->join('box_user d on a.user_id = d.id')->where('a.cashtype=4')->order("a.addtime desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->currentpage = $Page->firstRow + 1; 
		//print_r($this->list);
		$this->display();
	}

	public function returnfee(){
		$condition['order_num']=I('order_num');
		$data['refund_id']=I('refund_id');
		$data['status']=3;//已退
		$data['returntime']=time();
		$save = M('UserOrderDetail')->where($condition)->save($data);
		$openid = M('UserOrderDetail')->where($condition)->getField('openid');
		if($save){//必须有影响的行数
			$condition['is_frozen']=2;//被冻结的
		        	$points = M('UserPoints')->where($condition)->getField('points');
		        	$condition['isreturn']=array('neq',1);
		        	$save = M('UserPoints')->where($condition)->save(array('isreturn'=>1));
		        	if($save){
		        		$this->userpoints(99,$openid,1,$points,1,$condition['order_num']);
		        		//M('MedicalRecordsDiscuss')->where("order_num={$condition['order_num']}")->delete();//删除评论
		        		M('MedicalRecordsDiscuss')->where("order_num={$condition['order_num']}")->save(array('cashtype'=>5,'pubtype'=>3,'masktime'=>time()));//退费后不显示评论，5为已退费，屏蔽时间
		        		$json['status']=1;
		        	}else{
		        		$json['status']=1;
		        	}		
		}else{
			$json['status']=1;
		}

		$this->ajaxReturn($json,"JSON");
	}

	private function userpoints($type,$openid,$add,$points=0,$is_frozen=1,$order_num=0){
	        $data['openid']=$openid;
	        if(!$points){
	            $data['points'] = M('UserPointsRule')->where("id={$type}")->getField('points');//获得金豆规则对应的金豆 
	        }else{
	            $data['points'] = $points; 
	        }
	        $data['is_frozen']=$is_frozen;
	        $data['order_num']=$order_num;
	        $data['type']=$type;
	        $points = M('UserPoints')->add($data);//插入金豆详细
	        //echo M('UserPoints')->getLastSql();
	        if(($add==1) && ($is_frozen==1)){//金豆不冻结
	            M('User')->where("openid='{$openid}'")->setInc('points',$data['points']);//更新用户全局金豆
	            //echo M('User')->getLastSql();
	            return 1;
	        }elseif(($add!=1)){
	            $isDec = M('User')->where("openid='{$openid}' AND points > 0")->setDec('points',$data['points']);
	            return 1;
	        }
	}
}